/**

 * @author admin

 * 2014年7月16日

 */

package com.tmall.testgen.plugin.wizards.runinspring.listener;

import java.lang.reflect.Method;

import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Display;

import com.tmall.testgen.client.runinspring.RunInSpringHandler;
import com.tmall.testgen.client.runinspring.RunInSpringUtil;
import com.tmall.testgen.common.bo.interact.back.InterceptInfo;
import com.tmall.testgen.plugin.ConsoleFactory;
import com.tmall.testgen.plugin.views.vo.SpringTree;
import com.tmall.testgen.plugin.wizards.runinspring.RunInSpringWizadPage;

public class ComputeBtnSelectListener implements SelectionListener {

	
	private Method testMethod;
	private SpringTree parasRoot;
	private SpringTree resultRoot;
	private RunInSpringWizadPage page;
	

	public ComputeBtnSelectListener(RunInSpringWizadPage page) {
		this.testMethod = page.getTestMethod();
		this.parasRoot = page.getParasRoot();
		this.resultRoot = page.getResultRoot();
		this.page = page;
	}
	@Override
	public void widgetSelected(SelectionEvent e) {

		this.testMethod = page.getTestMethod();
		this.parasRoot = page.getParasRoot();
		this.resultRoot = page.getResultRoot();

			 
			new Thread(new Runnable(){

				@Override
				public void run() {
					InterceptInfo info=null;
					try {
						info = RunInSpringHandler.getInstance().getResult(testMethod, page.getMethodName(), parasRoot);
					} catch (Exception e) {
						ConsoleFactory.putError(e);
						return;
					}
					page.setCurrentInfo(info);

					resultRoot.removeAllChildren();
					Object result = info.getException() != null ? info.getException()
							: info.getResult();

					SpringTree resultTreee = new SpringTree();
					if(info.getException()!=null){
					    resultTreee.setLeafNode(true);
					    resultTreee.setName(info.getException());
					}else{
					    resultTreee.setLeafNode(result==null||RunInSpringUtil.isBasicType(result.getClass()));
	                    resultTreee.setValue(result);
	                    resultTreee.setClazz(result != null ? result.getClass() : null);
	                    resultTreee.setFieldName("result");
	                    resultTreee.initName();
					}
					
					//resultTreee.addFiledName();
					resultRoot.addChild(resultTreee);
					
					 Display.getDefault().syncExec(new Runnable() {
						 public void run() {
							 page.getResultTreeViewer().refresh();
						 }
					 }); 
					 

					
				}
				
			}).start();
			
		//page.getShell().setMinimized(false);
	}

	@Override
	public void widgetDefaultSelected(SelectionEvent e) {

	}
}
